<?php
namespace app\home\controller;
use think\Db;
class Build extends Base
{
    /**
     * 租房数据
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function homePage(){
        extract(input());
        $search = input('param.search/a');
        $page = input('param.page');
        if(!$page){
            $pages = 0;
        }else{
            $pages = $page-1;
        }
        if(!session('?my_location_city_id')){
            $shi = 1002;
        }else{
            $shi = session('my_location_city_id');
        }
        $search = $this->search($search, $page);
        //搜索条件
        $map = [];
        //1、区域筛选
        if($search[0] != ""){
            $map['b_qu'] = $search[0];
        }else{
            $map['b_shi'] = $shi;
        }
        //2、价格筛选:不限都为空，第一档的最小传1
        if($search[1] != ""){
            $jiage = [
                '0,50000',
                '0,30',
                '30,60',
                '60,90',
                '90,120',
                '120,150',
                '150,200',
                '200,300',
                '300,500',
                '500,50000'
            ];
            $map['b_price|b_price_max'] = array('between',$jiage[$search[1]]);
        }
        //3、面积筛选：也判断一下
        if($search[2] != ""){
            $mianji = [
                '0,50000000',
                '0,50',
                '50,80',
                '80,110',
                '110,150',
                '150,50000000'
            ];
            $map['b_mianji_min|b_mianji_max'] = array('between',$mianji[$search[2]]);
        }

        $map['b_status'] = 1;
        $data = $data = Db::table('up_building b')
            ->join ('up_area a','b.b_shi = a.district_id','LEFT')->where($map)
            ->field('b_id,b_title,district,b_title_bottom,b_img,b_mianji_min,b_mianji_max,b_unit_price,b_is_sale,b_huxing,b_hot_title,b_address')
            ->order('b_tuijiang desc,b_time desc')
            ->paginate(25);
        $qus = Db::table('up_area')
            ->where('level',3)
            ->where('pid',$shi)
            ->field('district_id,district')
            ->select();
        //----------------------------
        $types = [1=>'免税', 2=>'精装',3=>'学区房',4=>'配套完善',5=>'低首付',6=>'方便停车',7=>'环境优美',8=>'急售',9=>'婚房'];
        //----------------------------
        $fetch = [
            'data'=>$data,'pages'=>$pages,'search'=>$search,
            'types'=>$types, 'qus'=>$qus
        ];
        return $this->fetch ('build/index',$fetch);
    }
    /**
     * 楼盘详情
     *  shi：当前定位的市id
     */
    public function buildInfo(){
        extract(input());
        $where = array();
        $where['b_id'] = $b_id;
        $data = Db::table('up_building')
            ->where($where)
            ->find ();
        if(!$data || $data['b_status'] == 2){
            return json(['status'=>100,'data'=>'此房源不存在或已下架！']);
        }
        //轮播图
        if($data['b_images']){
            $data['b_images'] = explode(',',$data['b_images']);
        }
        //标签
        if($data['b_sign']){
            $data['b_sign'] =  Db::table('up_build_signs')
                ->where('bs_id','in',$data['b_sign'])
                ->page(1,5)
                ->column('bs_name');
        }
        //贷款
        $dknl = Db::table('up_about')->where('id',4)->value('content');
        $mww_dk =  new \app\api\controller\Show();
        $res = $mww_dk->debx(360,($data['b_price'] - $data['b_shoufu'])*10000,$dknl);
        $data['b_yuegong'] = $res['data'][0]['month'];
        //户型图和推荐
        $huxings = Db::table('up_district_hu')
            ->where('dh_type',2)
            ->where('dh_dis_id',$b_id)
            ->page(1,4)
            ->select();
        $builds = Db::table('up_building')
            ->where('b_status',1)
            ->order('b_tuijiang desc,b_updatetime desc')
            ->field('b_id,b_title,b_title_bottom,b_img,b_mianji_min,b_mianji_max,b_unit_price,b_is_sale,b_huxing,b_hot_title,b_address')
            ->page(1,4)
            ->select();
//        置业顾问
        $tjuser = Db::table('up_u_user')
            ->where('user_type',2)
            ->where('user_status',1)
            ->where('user_id','in',$data['b_user_ids'])
            ->page(1,4)
            ->field('user_id,user_username,user_shop,user_image,user_phone,user_ercode')
            ->select();
        $zx_img = Db::table('up_about')
            ->where('id',6)
            ->value ('content');
        $fetch = ['data'=>$data,'huxings'=>$huxings,'builds'=>$builds,'tjuser'=>$tjuser,'zx_img'=>$zx_img];
        return $this->fetch ('build/info',$fetch);
    }
    /**
     * 卖房
     */
    public function form_mf(){
        if(request()->isPost ()){
            extract(input());
            $user_id = session('user_id');
            if(preg_match("/^1[3456789]\d{9}$/", $phone)) {
                $data = array();
                $data['city'] = $city;
                $data['xiaoqu'] = $xiaoqu;
                $data['house'] = $house;
                $data['money'] = $money;
                $data['phone'] = $phone;
                $data['name'] = $name;
                $data['sex'] = $sex;
                $data['time'] = date('Y-m-d H:i:s');
                $data['uid'] = $user_id;
                $res = Db::table('up_form')->insert($data);
                if ($res) {
                    return json(['code' => 200, 'msg' => '提交成功']);
                } else {
                    return json(['code' => 100, 'msg' => '提交失败']);
                }
            }else{
                return json(['code' => 100, 'msg' => '请输入正确的手机号！']);
            }
        }
        $adminPhone = Db::table('up_about')
            ->where('id',7)
            ->value ('content');
        $this->assign('adminPhone',$adminPhone);
        return $this->fetch();
    }
    /**
     * 装修
     */
    public function form_zx(){
        if(request()->isPost ()){
            extract(input());
            if(preg_match("/^1[3456789]\d{9}$/", $phone)) {
                $user_id = session('user_id');
                $data = array();
                $data['city'] = $city;
                $data['mianji'] = $mianji;
                $data['shi'] = $shi;
                $data['wei'] = $wei;
                $data['ting'] = $ting;
                $data['yusuan'] = $yusuan;
                $data['phone'] = $phone;
                $data['name'] = $name;
                $data['sex'] = $sex;
                $data['time'] = date('Y-m-d H:i:s');
                $data['uid'] = $user_id;
                $res = Db::table('up_form_fit')->insert($data);
                if ($res) {
                    return json(['code' => 200, 'msg' => '提交成功']);
                } else {
                    return json(['code' => 100, 'msg' => '提交失败']);
                }
            }else{
                return json(['code' => 100, 'msg' => '请输入正确的手机号！']);
            }
        }
        $adminPhone = Db::table('up_about')
            ->where('id',7)
            ->value ('content');
        $this->assign('adminPhone',$adminPhone);
        return $this->fetch();
    }
}